
package nl.leidenuniv.hci.team5;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.view.MotionEvent;

import android.widget.Toast;

public class TouchBallView extends View {
	
	//--touch
	private float previousX;
	private float previousY;
	
	//--ball
	private float ballX;
	private float ballY;
	private float ballRadius = 50;
	private RectF ballBounds;
	private Paint paint;
	
	Context context;
	
	//--constructor-----------------------------------------
	public TouchBallView(Context _context) {
	
		//--super
		super(_context);
		context = _context;
		this.setFocusableInTouchMode(true);
		
		//--ball
		ballBounds = new RectF();
		paint = new Paint();
		paint.setTypeface(Typeface.MONOSPACE);
		paint.setTextSize(18);
		
	}
	
	//--touch input handler-----------------------------------
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		ballX = event.getX();
		ballY = event.getY();
		
		//--toast
		//Toast.makeText(context, "x = " + ballX + ", y =" + ballY, 1).show();

		return true;
	}
	
	//--draw------------------------------------------------
	@Override
	protected void onDraw(Canvas canvas) {
		ballBounds.set(this.ballX - this.ballRadius, this.ballY - this.ballRadius, this.ballX + this.ballRadius, this.ballY + this.ballRadius);
		paint.setColor(Color.CYAN);
		canvas.drawOval(ballBounds, paint);
		
		//--message
		paint.setColor(Color.RED);
		canvas.drawText("x: " + (int) ballX + ", y: " + (int) ballY, ballX - ballRadius - 20 , ballY - ballRadius - 20, paint);
		
		//--delay
		try {
			Thread.sleep(0);
		} catch (InterruptedException e) { }	
		
		invalidate(); // force re-draw
	}
	
	
}